package project.active;

import model.pojo.Employee;
import project.dao.pr_projectDAO;
import project.model.PrProject;
import project.model.PrTestProject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import approve.action.R;

public class Projectdetail_addnewerror_tc extends Activity {

	EditText txttitle_content,txtcontent;
	TextView lbtitle,lbcontent;
	Button btok,btcancel;
	RadioButton rdnofix,rderror,rdfixcomplete;
	
	String namecolor="#B023AB";//mau tim
	String lang = "EN",database="admin";
	PrProject pro=new PrProject();
	PrTestProject err=new PrTestProject();
	Employee emp=new Employee();
	pr_projectDAO prdao=new pr_projectDAO();
	Bundle language;
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.projectdetail_addnewerror_tc);

		language=getIntent().getExtras().getBundle(getLang());
		
		Intent i=this.getIntent();
		pro=(PrProject)i.getSerializableExtra("project");
		err=(PrTestProject)i.getSerializableExtra("errorproject");
		emp=(Employee)i.getSerializableExtra("em");
		
		getIdComponent();

		btok.setOnClickListener(clickListenerOK);
		btcancel.setOnClickListener(clickListenerCancel);
		
		rdnofix.setOnClickListener(colorNoFix);
		rderror.setOnClickListener(colorError);
		rdfixcomplete.setOnClickListener(colorFixComplete);
		
		changeLanguage();
	}

	private OnClickListener colorFixComplete=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			namecolor="#3DA850";
			Spannable WordtoSpan = new SpannableString(txttitle_content.getText().toString());        
			WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, txttitle_content.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			txttitle_content.setText(WordtoSpan);
		}
	};
	private OnClickListener colorError=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			namecolor="#B02323";
			Spannable WordtoSpan = new SpannableString(txttitle_content.getText().toString());        
			WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, txttitle_content.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			txttitle_content.setText(WordtoSpan);
		}
	};
	private OnClickListener colorNoFix=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			namecolor="#B023AB";
			Spannable WordtoSpan = new SpannableString(txttitle_content.getText().toString());        
			WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, txttitle_content.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			txttitle_content.setText(WordtoSpan);
		}
	};
	
	private void getIdComponent(){
		txttitle_content=(EditText)findViewById(R.id.txttitle_content);
		txtcontent=(EditText)findViewById(R.id.txtcontent);
		btok=(Button)findViewById(R.id.btok);
		btcancel=(Button)findViewById(R.id.btcancel);
		rdnofix=(RadioButton)findViewById(R.id.rdnofix);
		rderror=(RadioButton)findViewById(R.id.rderror);
		rdfixcomplete=(RadioButton)findViewById(R.id.rdfixcomplete);
	}
	
	private OnClickListener clickListenerCancel=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showListenErrorCancel();
		}
	};
	public void showListenErrorCancel(){
		Intent i=new Intent(this,Projectdetail_tab_tc.class);
		if(i!=null)
			loadIntent(i);
	}
	private OnClickListener clickListenerOK=new OnClickListener() {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showListError(txttitle_content.getText().toString(),txtcontent.getText().toString());
		}
	};
	private void showListError(String title,String content){
		if(title==null||title.equalsIgnoreCase("")||content==null||content.equalsIgnoreCase("")){
			return;
		}
		else {
			prdao.addNewCommentErrorDAO_tc(getDatabase(),pro.getIdProject(),emp.getIdemployee(),title,namecolor,content);
		}
		Intent i=new Intent(this,Projectdetail_tab_tc.class);
		if(i!=null)
			loadIntent(i);
	}
	
	private void loadIntent(Intent i){
		i.putExtras(getIntent().getExtras());
		i.putExtra("lang", lang);
		i.putExtra("project", pro);
		i.putExtra("errorproject", err);
		i.putExtra("em", emp);
		i.putExtra("database", getDatabase());
		startActivity(i);
	}
	
	private void changeLanguage(){
		lbtitle=(TextView)findViewById(R.id.lbtitle);
		lbcontent=(TextView)findViewById(R.id.lbcontent);
		
		lbtitle.setText(language.getString("title"));
		lbcontent.setText(language.getString("content"));
		btok.setText(language.getString("ok"));
		btcancel.setText(language.getString("cancel"));
	}
}
